package com.shop.controller;

import com.shop.common.ResponseResult;
import com.shop.entity.Role;
import com.shop.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@CrossOrigin
@RestController
public class RoleController {
    @Autowired
    private RoleService roleService;
    @GetMapping("roles/{id}")
    public ResponseResult selectOne(@PathVariable Integer id){
        return roleService.selectOne(id);
    }
    @PostMapping("roles")
    public ResponseResult addRole(@RequestBody Role role){
        return roleService.addRole(role);
    }
    @PutMapping("roles/{id}")
    public ResponseResult updateRole(@PathVariable Integer id,@RequestBody Role role){
        return roleService.updateRole(id,role);
    }
    @DeleteMapping("roles/{id}")
    public ResponseResult deleteRole(@PathVariable Integer id){
        return roleService.deleteRole(id);
    }
    @GetMapping("roles")
    public ResponseResult selectRole(){
        return roleService.selectAll();
    }
    @PostMapping("roles/{roleId}/rights")
    public ResponseResult authorization(@PathVariable Integer roleId, @RequestBody Map map){
        ResponseResult result = new ResponseResult();
        String rids="";
        try {
             rids=map.get("rids").toString();
        }catch (Exception e){
            result.setBADREQUEST();
            return result;
        }
        return roleService.updateAuthorization(roleId,rids);
    }
    @DeleteMapping("roles/{roleId}/rights/{rightId}")
    public ResponseResult deleteAuthorization(@PathVariable Integer roleId,@PathVariable Integer rightId){
        return roleService.deleteAuthorization(roleId,rightId);
    }

}
